package com.example.pet.controller;

import com.example.pet.beans.UserInfo;
import com.example.pet.common.Result;
import com.example.pet.server.WebSocketServer;
import com.example.pet.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * @author liukai
 * @version 1.0.0
 * @ClassName UserController.java
 * @Description TODO
 * @createTime 2021年04月20日 15:30:00
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private IUserInfoService iUserInfoService;

    @Autowired
    private WebSocketServer webSocketServer;

    @RequestMapping("/hello")
    public String hello(){
        System.out.println(UUID.randomUUID().toString().substring(0,8));
        return "hello";
    }

    /**
     * 登录
     * @param userInfo
     * @return
     */
    @PostMapping(value = "/login")
    public Result login(@RequestBody UserInfo userInfo){
        System.out.println(userInfo.toString());
        return iUserInfoService.login(userInfo);
    }

    /**
     * 注册
     * @param userInfo
     * @return
     */
    @PostMapping(value = "/register")
    public Result register(@RequestBody UserInfo userInfo){
        return iUserInfoService.register(userInfo);
    }

    @GetMapping(value = "/socket")
    public String send(){
        webSocketServer.sendInfo("10086","12345");
        return "12345";
    }

    /**
     * 根据用户id查找用户名
     * @param userId
     * @return
     */
    @GetMapping(value = "/getUserNameById/{userId}")
    public Result getUserNameById(@PathVariable("userId") Integer userId){
        return iUserInfoService.findUserNameById(userId);
    }

}
